import 'package:family_expense/features/bank_card/models/bank.dart';
import 'package:my_flutter_core/my_flutter_core.dart';

class BankCard {
  BankCard({
    this.id,
    this.name,
    this.statementDate,
    this.paymentDueDate,
    this.customerServiceNumber,
    this.isCreditCard = true,
    this.enabled = true,
    this.bank,
  });

  factory BankCard.fromJson(Map<String, dynamic> json) {
    return BankCard(
      id: json['id']?.toString(),
      name: json['name']?.toString(),
      statementDate: json['statementDate']?.toString(),
      paymentDueDate: json['paymentDueDate']?.toString(),
      customerServiceNumber: json['customerServiceNumber']?.toString(),
      isCreditCard: tryToConvert<bool>(json['isCreditCard'], (p) => p as bool),
      enabled: tryToConvert<bool>(json['enabled'], (p) => p as bool),
      bank: Bank(
        id: json['bankId']?.toString(),
        name: json['bankName']?.toString(),
        shortName: json['bankShortName']?.toString(),
        logoUrl: json['bankLogoUrl']?.toString(),
      ),
    );
  }

  static const nameMaxLength = 50;
  static const statementDateMaxLength = 50;
  static const paymentDueDateMaxLength = 50;
  static const customerServiceNumberMaxLength = 50;

  String? id;
  String? name;
  String? statementDate;
  String? paymentDueDate;
  String? customerServiceNumber;
  bool? isCreditCard;
  bool? enabled;
  Bank? bank;

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'statementDate': statementDate,
      'paymentDueDate': paymentDueDate,
      'customerServiceNumber': customerServiceNumber,
      'isCreditCard': isCreditCard,
      'enabled': enabled,
      'bank': {
        'id': bank?.id,
        'name': bank?.name,
        'shortName': bank?.shortName,
        'logoUrl': bank?.logoUrl,
      },
    };
  }

  BankCard clone() {
    return BankCard(
      id: id,
      name: name,
      statementDate: statementDate,
      paymentDueDate: paymentDueDate,
      customerServiceNumber: customerServiceNumber,
      isCreditCard: isCreditCard,
      enabled: enabled,
      bank: bank?.clone(),
    );
  }
}
